<title>发票分类</title>
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <div style="padding-bottom: 10px;">
        <button class="layui-btn layuiadmin-btn-fpfl mybtn" data-type="batchdel">删除</button>
        <button class="layui-btn layuiadmin-btn-fpfl mybtn" data-type="add">添加</button>
        <button type="button" class="layui-btn mybtn" id="invoice" style="margin-right: 10px">批量添加</button>
        <button class="layui-btn layuiadmin-btn-fpfl mybtn" lay-filter="LAY-fpfl-front-search"
                data-type="download"
        >
          下载模板
        </button>
        <button class="layui-btn layuiadmin-btn-fpfl mybtn" lay-filter="LAY-fpfl-front-search" data-type="search"
                style="float: right;margin: 0">
          <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
        </button>
        <input onfocus="" style="width: 200px; float: right" type="text"
               name="fpflinfo"
               value="" placeholder="请输入要查询的商品名称" autocomplete="off" class="layui-form layui-input">
      </div>


      <table id="LAY-user-fpfl" lay-filter="LAY-user-fpfl"></table>

      <script type="text/html" id="table-fpfl">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                class="layui-icon layui-icon-edit"></i>编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                class="layui-icon layui-icon-delete"></i>删除</a>
      </script>
    </div>
  </div>
</div>


<script>
  layui.use(['table', 'form','upload'], function () {
    var table = layui.table
        , form = layui.form
        , admin = layui.admin
        , view = layui.view
        , upload = layui.upload
        , $ = layui.$
    let myAuthorization = layui.data('layuiAdmin', 'Authorization')
    upload.render({
            elem: '#invoice'
            , url: layui.setter.hostAddr + '/portal/fp/uploadfp'
            , headers: {"Authorization": myAuthorization}
            , accept: 'file' //普通文件
            , before: function (obj) {//参数
                loadIndex = layer.load(2, {shade: [0.15, '#ccc']});
            }
            , done: function (res) {
                layer.closeAll('loading');
                if (res.code === 0) {
                    doSuccess()
                } else {
                    doFail()
                }
                table.reload('LAY-user-fpfl');
            }
        });
    table.set({
      headers: { //通过 request 头传递
        Authorization: layui.data('layuiAdmin').Authorization
      }
    });
    table.render({
      elem: "#LAY-user-fpfl"
      // , url: './json/fpfl/fpfl_test.js'
      , url: layui.setter.hostAddr + '/portal/fp/fplist'
      , cols: [[
        {type: 'checkbox', fixed: 'left'}
        , {field: 'spmc', width: 200, title: '商品名称', align: 'center'}
        , {field: 'spbm', title: '商品编码', minWidth: 140, align: 'center'}
        , {field: 'flmc', title: '分类名称', minWidth: 140, align: 'center'}
        , {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-fpfl'}
      ]]
      , page: true
      // , height: 'full-320'
      , text: '加载出现异常！'
    })
    //监听工具条
    table.on('tool(LAY-user-fpfl)', function (obj) {
      var fpfldata = obj.data, delobj = [];
      delobj.push(fpfldata.spmc)
      if (obj.event === 'del') {
        layer.prompt({
          formType: 1
          , title: '敏感操作，请验证口令'
        }, function (value, index) {
          var del_data = {id: index, passcode: value}
          // console.log(del_data)
          layer.close(index);

          layer.confirm('真的删除行么', function (index) {
            $.ajax({
              url: layui.setter.hostAddr + "/portal/fp/fp",
              dataType: 'json',
              data: JSON.stringify(delobj),
              async: false,
              beforeSend: function (XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("Authorization", layui.data('layuiAdmin').Authorization);
                XMLHttpRequest.setRequestHeader("Content-Type", "application/json");
              },
              type: 'DELETE',
              success: function (res) {
                // console.log("删除结果====" + JSON.stringify(res))
                doSuccess()
              },
              error: function (XMLHttpRequest, textStatus, errorThrown) {
                //通常情况下textStatus和errorThrown只有其中一个包含信息
                // console.log(textStatus);   //调用本次ajax请求时传递的options参数
                // console.log(errorThrown);   //调用本次ajax请求时传递的options参数
                doFail()
              }

            })
            obj.del();
            table.reload('LAY-user-fpfl');
            layer.close(index);
          });
        });
      } else if (obj.event === 'edit') {
        admin.popup({
          title: '编辑分类信息'
          , area: ['500px', '450px']
          // , id: 'LAY-popup-user-add'
          , success: function (layero, index) {
            view(this.id).render('DataCollectProcess/BasicInformationCollection/invoice_classification/invoice', fpfldata).done(function () {
              // view(this.id).render('user/user/userform', data).done(function(){
              form.render(null, 'layuiadmin-form-fpfl');

              //监听提交
              form.on('submit(LAY-fpfl-front-submit)', function (data) {
                var editfield = data.field; //获取提交的字段
                editfield['beforeSpmc'] = fpfldata.spmc
                // console.log(JSON.stringify(editfield))
                // console.log(JSON.stringify(editfield))
                debugger
                //提交 Ajax 成功后，关闭当前弹层并重载表格
                $.ajax({
                  url: layui.setter.hostAddr + "/portal/fp/fp",
                  // dataType: 'json',
                  data: JSON.stringify(editfield),
                  contentType: "application/json",
                  async: false,
                  beforeSend: function (XMLHttpRequest) {
                    XMLHttpRequest.setRequestHeader("Authorization", layui.data('layuiAdmin').Authorization)
                  },
                  type: 'PUT',
                  success: function (res) {
                    // console.log("编辑结果====" + JSON.stringify(res))
                    doSuccess()
                  },
                  error: function (XMLHttpRequest, textStatus, errorThrown) {
                    //通常情况下textStatus和errorThrown只有其中一个包含信息
                    // console.log(textStatus);   //调用本次ajax请求时传递的options参数
                    // console.log(errorThrown);   //调用本次ajax请求时传递的options参数
                    doFail()
                  }

                })
                layui.table.reload('LAY-user-fpfl'); //重载表格
                layer.close(index); //执行关闭
              });
            });
          }
        });
      }
    });


    var active = {
      batchdel: function () {
        debugger
        var checkStatus = table.checkStatus('LAY-user-fpfl')
            , checkData = checkStatus.data, delobjs = []; //得到选中的数据

        if (checkData.length === 0) {
          return layer.msg('请选择数据666');
        }
        for (let i = 0; i < checkData.length; i++) {
          delobjs.push(checkData[i].spmc)
        }
        // console.log(delobjs)
        // console.log("要删除的数据====" + JSON.stringify(checkData))
        layer.prompt({
          formType: 1
          , title: '敏感操作，请验证口令'
        }, function (value, index) {
          layer.close(index);

          layer.confirm('确定删除吗？', function (index) {

            //执行 Ajax 后重载
            $.ajax({
              url: layui.setter.hostAddr + `/portal/fp/fp`,
              dataType: 'json',
              data: JSON.stringify(delobjs),
              async: false,
              beforeSend: function (XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("Authorization", layui.data('layuiAdmin').Authorization);
                XMLHttpRequest.setRequestHeader("Content-Type", "application/json");

              },
              type: 'DELETE',
              success: function (res) {
                // console.log("删除结果====" + JSON.stringify(res))
                doSuccess()
              },
              error: function (XMLHttpRequest, textStatus, errorThrown) {
                //通常情况下textStatus和errorThrown只有其中一个包含信息
                // console.log(textStatus);   //调用本次ajax请求时传递的options参数
                // console.log(errorThrown);   //调用本次ajax请求时传递的options参数
                doFail()
              }

            })
            table.reload('LAY-user-fpfl');
            // layer.msg('已删除');
          });
        });
      }
      , add: function () {
        admin.popup({
          title: '添加分类信息'
          , area: ['500px', '450px']
          , success: function (layero, index) {
            view(this.id).render('DataCollectProcess/BasicInformationCollection/invoice_classification/invoice').done(function () {
              form.render(null, 'layuiadmin-form-fpfl');
              //监听提交
              form.on('submit(LAY-fpfl-front-submit)', function (data) {
                // form.on('submit(*)', function(data){
                var field = data.field; //获取提交的字段
                // console.log("添加的发票分类字段 ===" + JSON.stringify(field))
                $.ajax({
                  url: layui.setter.hostAddr + "/portal/fp/fp",
                  data: JSON.stringify(field),
                  contentType: "application/json",
                  dataType: "json",
                  async: false,
                  beforeSend: function (XMLHttpRequest) {
                    XMLHttpRequest.setRequestHeader("Authorization", layui.data('layuiAdmin').Authorization);
                  },
                  type: 'POST',
                  success: function (res) {
                    doSuccess()
                  },
                  error: function (XMLHttpRequest, textStatus, errorThrown) {
                    //通常情况下textStatus和errorThrown只有其中一个包含信息
                    // console.log(textStatus);   //调用本次ajax请求时传递的options参数
                    // console.log(errorThrown);   //调用本次ajax请求时传递的options参数
                    doFail()
                  }
                })
                // alert("添加岗位的数据===" + JSON.stringify(field))
                layui.table.reload('LAY-user-fpfl'); //重载表格
                layer.close(index); //执行关闭
              });
            });
          }
        });
      }
      , download: function () {
                debugger
                var curWwwPath = window.document.location.href;
                var pathName = window.document.location.pathname;
                var pos = curWwwPath.indexOf(pathName);
                var localhostPath = curWwwPath.substring(0, pos);
                var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
                var url = localhostPath + projectName + "/template/涉农纳税人信息.xlsx";
                window.open(layui.setter.static + '/template/fpfl.xlsx');
            }
      , search: function () {
        var fpflinfo = $("input[name='fpflinfo']").val()

        //执行重载
        table.reload('LAY-user-fpfl', {
          page: {
            curr: 1 //重新从第 1 页开始
          }
          , where: {
            keyword: fpflinfo
          }
        });
      }
    };
    $('.layui-btn.layuiadmin-btn-fpfl ').on('click', function () {
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });

  })


</script>
